package com.wonders.qxmanager.dao.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wonders.qxmanager.base.BaseDao;
import com.wonders.qxmanager.base.WidgetPage;
import com.wonders.qxmanager.dao.OrganizationDao;
import com.wonders.qxmanager.model.Organization;
import com.wonders.qxmanager.model.Team;
import com.wonders.qxmanager.utils.PageArrayList;

@Repository
public class OrganizationDaoImpl extends BaseDao<Organization, Serializable> implements OrganizationDao{

	/**
	 * 新增组织机构
	 */
	@Override
	public void addOrg(Organization org) {
		save(org);
		
	}
	
	/**
	 * 修改对象
	 */
	@Override
	public void updateOrg(Organization org) {
		saveOrUpdate(org);
		
	}

	/**
	 * 删除对象
	 */
	@Override
	public boolean deleteOrg(int id) {
		 
		return deleteById(id);
	}

	/**
	 * 分页
	 */
	@Override
	public PageArrayList<Organization> getListByPage(Organization org,
			WidgetPage page) {
		Criteria criteria = getSession().createCriteria(Organization.class);
		if (org.getId()!= null)
			criteria.add(Restrictions.like("id", "%" + org.getId()
					+ "%"));
		if (org.getName() != null)
			criteria.add(Restrictions.like("name", "%" + org.getName()
					+ "%"));
		int total = ((Long) criteria.setProjection(Projections.rowCount())
				.uniqueResult()).intValue();
		criteria.setResultTransformer(Criteria.ROOT_ENTITY);
		criteria.setProjection(null);
		int startIndex = page.getStartOfPage();
		criteria.setFirstResult(startIndex - 1);
		criteria.setMaxResults(page.getPageSize());
		List<Organization> list = criteria.list();
		PageArrayList<Organization> pageList = new PageArrayList<Organization>(total,
				page.getPageSize(), page.getCurrentPage());
		pageList.addAll(list);
		return pageList;
	}

	/**
	 * 获取所有对象	
	 */
	@Override
	public List<Organization> getAll() {
		// TODO Auto-generated method stub
		return getListByHQL("from Organization", null);
	}
	
	/**
	 * 根据id获取单个对象
	 */
	@Override
	public Organization getOrgById(BigDecimal id) {
		// TODO Auto-generated method stub
		return get(id);
	}

	@Override
	public List<Organization> getAll(String sqlString) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
